package edu.scau.nutzDao.userDaoImpl;

import java.util.List;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;

import edu.scau.nutzDao.dbc.DatabaseConnection;
import edu.scau.nutzDao.userDao.UserDAO;
import edu.scau.nutzDao.vo.User;

public class UserDaoImpl implements UserDAO {

	 private Dao dao=null;
	User user=null;
	DatabaseConnection db=new DatabaseConnection();
	public UserDaoImpl(){
		dao=db.getDao();
	}
	
	@Override
	public boolean save(User user) throws Exception {
		user.setUserName(user.getUserName());
		user.setPassword(user.getPassword());
		if(dao.insert(user)!=null)
			return true;
		return false;
	}

	@Override
	public boolean delete(User user) throws Exception {
		if(dao.delete(user)!=-1)
			return true;
		return false;
	}

	@Override
	public boolean update(User user) throws Exception {
		if(dao.update(user)>0)
			return true;
		return false;
	}

	@Override
	public User findById(int userId) throws Exception {
		if(userId>0){
		    user=dao.fetch(User.class,Cnd.where("userID","=","userId"));
		    return user;
		}
		   return null;
	}

	@Override
	public User findByUserName(String userName) throws Exception {
		if(userName!=null){
			user=dao.fetch(User.class,Cnd.where("userName","=","userName"));
			return user;
		}
		return null;
	}

	@Override
	public List<User> findAll() throws Exception {
		List<User> user=dao.query(User.class, null);
		return user;
	}

}
